//
//  RBShopVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/5/26.
//

#import "RBShopVC.h"
#import "RBSearchVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RanTool.h"
#import "RBPager.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "UIViewController+RBAdd.h"
#import "RBShopListCell.h"
#import "RBShopDetailVC.h"
#import "RBRefreshHeader.h"
#import "RBRefreshFooter.h"
#import "RBShopResponse.h"

@interface RBShopVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,UITextFieldDelegate>

@property(nonatomic, strong)IBOutlet UIView *searchView;
@property(nonatomic, strong)IBOutlet UITextField *searchF;
@property(nonatomic, strong)UIView *filterView;
@property(nonatomic, strong)UIView *compositeFilterBtn;
@property(nonatomic, strong)UIView *salesFilterBtn;
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSource;

@property(nonatomic, strong) RBShopResponse *response;
@property(nonatomic, assign) NSInteger filterIndex;//0综合 1销量 2活动
@property(nonatomic, assign) NSInteger filterState;//1 desc 2 asc
@property(nonatomic, copy) NSString *searchText;

@end

@implementation RBShopVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.fd_prefersNavigationBarHidden = YES;
    
    [self.searchView zoomSubViewsWithSelf];
    self.searchView.height = Zoom(self.searchView.height);
    [self.view addSubview:self.searchView];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(kRBStatusBarHeight + 15);
        make.height.mas_equalTo(self.searchView.height);
    }];
    self.filterView.hidden = NO;
    [self compositeFilerBtnTapped];
}

- (void)fillData
{
    [self.dataSource removeAllObjects];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.sectionInset = UIEdgeInsetsMake(Zoom(10), 0, Zoom(10), 0);
        group.minimumLineSpacing = Zoom(10);
        group.cellModels = NSMutableArray.array;
        group;
    })];
    [self appendData];
}

- (void)appendData
{
    NSMutableArray *arrayM = ({
        RBGroupModel *group = self.dataSource.lastObject;
        (NSMutableArray *)group.cellModels;
    });
    for (id model in self.response.data)
    {
        [arrayM addObject:({
            RBCellModel *cellModel = RBCellModel.new;
            cellModel.reuseCellIdentifier = NSStringFromClass(RBShopListCell.class);
            cellModel.cellData = model;
            cellModel;
        })];
    }
    [self.collectionView reloadData];
    if(!arrayM.count)
    {
        [self.collectionView showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
    }
}

#pragma mark - Network

- (NSString *)listPageKey
{
    return @"page";
}
- (NSDictionary *)listParams
{
    NSMutableDictionary *dicM = NSMutableDictionary.dictionary;
    dicM[@"store_name"] = self.searchText;
    if(self.filterIndex == 2)
    {
        dicM[@"type"] = @"huodong";
    }
    else
    {
        dicM[@"ord_name"] = self.filterIndex == 0? @"id": @"xiaoliang";
        dicM[@"ord_val"] = self.filterState == 1? @"desc": @"asc";
    }
    return dicM;
}
- (NSString *)listRequestPath
{
    return @"han_store_index";
}
- (NSInteger)ListCurrentPage
{
    return self.response.currentPage;
}
- (BOOL)listLog
{
    return YES;
}
- (BOOL)listPost
{
    return YES;
}
- (Class)listResponseClass
{
    return RBShopResponse.class;
}
- (UIScrollView *)listView
{
    return self.collectionView;
}

- (void)getFirstPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadFirstPageWithDesView:self.listView showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:^(id data) {
        
    } reloadBlock:^{
        [weak_self getFirstPageData];
    }];
}

- (void)getNextPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadNextPageWithSucBlock:^(id data) {
        weak_self.response = data;
        [weak_self appendData];
    } failBlock:^(id data) {
        
    }];
}

#pragma mark - Event

- (void)compositeFilerBtnTapped
{
    NSInteger tag = self.compositeFilterBtn.tag + 1;
    if(tag > 2)
    {
        tag = 1;
    }
    [self setFilterBtn:self.compositeFilterBtn withTag:tag];
    [self setFilterBtn:self.salesFilterBtn withTag:0];
    self.filterIndex = 0;
    self.filterState = tag;
    [self.collectionView.mj_header beginRefreshing];
}

- (void)salesFilerBtnTapped
{
    NSInteger tag = self.salesFilterBtn.tag + 1;
    if(tag > 2)
    {
        tag = 1;
    }
    [self setFilterBtn:self.salesFilterBtn withTag:tag];
    [self setFilterBtn:self.compositeFilterBtn withTag:0];
    self.filterIndex = 1;
    self.filterState = tag;
    [self.collectionView.mj_header beginRefreshing];
}

- (void)atyFilerBtnTapped
{
    [self setFilterBtn:self.compositeFilterBtn withTag:0];
    [self setFilterBtn:self.salesFilterBtn withTag:0];
    self.filterIndex = 2;
    self.filterState = 0;
    [self.collectionView.mj_header beginRefreshing];
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

kRBCollectionViewDataSourceAndDelegateAuto(self.dataSource);

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    self.searchText = textField.text;
    [self.listView.mj_header beginRefreshing];
    return YES;
}

#pragma mark - Setter

- (void)setFilterBtn: (UIView *)filterBtn withTag: (NSInteger)tag
{
    filterBtn.tag = tag;
    UIImageView *upImgView = (UIImageView *)[filterBtn viewWithTag:333];
    UIImageView *dowImgView = (UIImageView *)[filterBtn viewWithTag:444];
    if (tag == 0)
    {
        upImgView.image = [UIImage imageNamed:@"shopFilterUp"];
        dowImgView.image = [UIImage imageNamed:@"shopFilterDown"];
    }
    else if (tag == 2)
    {
        upImgView.image = [UIImage imageNamed:@"shopFilterUpSel"];
        dowImgView.image = [UIImage imageNamed:@"shopFilterDown"];
    }
    else if (tag == 1)
    {
        upImgView.image = [UIImage imageNamed:@"shopFilterUp"];
        dowImgView.image = [UIImage imageNamed:@"shopFilterDownSel"];
    }
        
}

#pragma mark - Getter

- (UIView *)filterView
{
    if (!_filterView)
    {
        self.filterView = ({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.top.mas_equalTo(self.searchView.mas_bottom);
                make.height.mas_equalTo(Zoom(37));
            }];
            
            UIStackView *stackView = UIStackView.new;
            [view addSubview:stackView];
            [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.mas_equalTo(0);
                make.left.mas_equalTo(Zoom(50));
                make.right.mas_equalTo(-Zoom(50));
            }];
            stackView.distribution = UIStackViewDistributionEqualSpacing;
            
            self.compositeFilterBtn = [self filterBtnWithTitle:@"综合"];
            [self.compositeFilterBtn rbAddTapGesWithTarget:self action:@selector(compositeFilerBtnTapped)];
            [stackView addArrangedSubview:self.compositeFilterBtn];
            
            self.salesFilterBtn = [self filterBtnWithTitle:@"销量"];
            [self.salesFilterBtn rbAddTapGesWithTarget:self action:@selector(salesFilerBtnTapped)];
            [stackView addArrangedSubview:self.salesFilterBtn];
            
            UILabel *atyBtn = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"活动"];
            [atyBtn rbAddTapGesWithTarget:self action:@selector(atyFilerBtnTapped)];
            [stackView addArrangedSubview:atyBtn];
            
            view;
        });
    }
    return _filterView;
}

- (UIView *)filterBtnWithTitle: (NSString *)title
{
    UIView *view = UIView.new;
    [view h_view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:title] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
    [view h_view_addView:({
        UIView *view = UIView.new;
        [view view_addView:({
            UIImageView *view = [RanTool imgViewWithMode:0 radius:0 imgName:@"shopFilterUp"];
            view.tag = 333;
            view;
        }) withHeight:Zoom(5) width:Zoom(7) top:0 bottom:0 left:0 right:0 centerX:0];
        [view view_addView:({
            UIImageView *view = [RanTool imgViewWithMode:0 radius:0 imgName:@"shopFilterDown"];
            view.tag = 444;
            view;
        }) withHeight:Zoom(5) width:Zoom(7) top:Zoom(2) bottom:0 left:0 right:0 centerX:0];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(6) right:0 centerY:YES];
    return view;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionView = ({
            UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
            layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
            UICollectionView *view = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.dataSource = self;
            view.delegate = self;
            view.backgroundColor = [RBThemeConfig bgGrayColor];
            view.mj_header = [RBRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
            view.mj_footer = [RBRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(getNextPageData)];
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.top.mas_equalTo(self.filterView.mas_bottom);
            }];
            view;
        });
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
    }
    return _dataSource;
}

@end
